﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using S.Services.SheepMale.Api.Application.Command.PurchaseCommand;
using S.Services.SheepMale.Api.Application.Command.SheepMaleCommand;
using S.Services.SheepMale.Api.Extensions;
using S.Services.SheepMale.Domain.PurchaseSales;
using System.IO;
using System.Security.Claims;

namespace S.Services.SheepMale.Api.Controllers
{
    /// <summary>
    /// 期初管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = nameof(ApiVersionInfo.期初管理))]
    public class SheepController : ControllerBase
    {
        private readonly IMediator _mediator;

        private readonly ILogger _logger;
        public SheepController(IMediator mediator, ILogger logger)
        {
            _mediator = mediator;
            _logger = logger;
        }
        /// <summary>
        /// 种公期初添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> HandleSheepAdd(SheepFatherAddCommand request, CancellationToken cancellationToken)
        {
            request.CreateBy = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            return Ok(await _mediator.Send(request));
        }
        /// <summary>
        /// 种公期初删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> HandleSheepDel([FromQuery] SheepFatherDelCommand request, CancellationToken cancellationToken)
        {
            return Ok(await _mediator.Send(request));
        }
        /// <summary>
        /// 种公批量删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> HandleSheepAllDel([FromQuery] SheepFatherAllDelCommand request, CancellationToken cancellationToken)
        {
            return Ok(await _mediator.Send(request));
        }
        /// <summary>
        /// 种公期初修改
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> HandleSheepUpd(SheepFatherUpdCommand request, CancellationToken cancellationToken)
        {
            request.UpdateBy = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            return Ok(await _mediator.Send(request));
        }
        /// <summary>
        /// 种公审核
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> HandleFatherExamine(SheepFatherExamineCommand request, CancellationToken cancellationToken)
        {
            return Ok(await _mediator.Send(request));
        }
        /// <summary>
        /// 种公重新审核
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> HandleFatherNewExamine(SheepFatherNewExamineCommand request, CancellationToken cancellationToken)
        {
            return Ok(await _mediator.Send(request));
        }
        /// <summary>
        /// 种母期初添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> HandleSheepUpdMom(SheepMomAddCommand request, CancellationToken cancellationToken)
        {
            request.CreateBy = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            return Ok(await _mediator.Send(request));
        }
        /// <summary>
        /// 种母期初批删
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> HandleAllDelMom([FromQuery] SheepMomAllDelCommand request, CancellationToken cancellationToken)
        {
            return Ok(await _mediator.Send(request));
        }
        /// <summary>
        /// 种母期初删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> HandleDelMom([FromQuery] SheepMomDelCommand request, CancellationToken cancellationToken)
        {
            return Ok(await _mediator.Send(request));
        }
        /// <summary>
        /// 种母期初修改
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> HandleUpdMom(SheepMomUpdCommand request, CancellationToken cancellationToken)
        {
            request.UpdateBy = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            return Ok(await _mediator.Send(request));
        }
        [HttpGet]
        public void Test()
        {
            try
            {
                string A = "qwe";
                int B = Convert.ToInt32(A);
                //ClientMarket Info = null;
                //Info.MarketNumber = "test";
                //return Ok();
            }
            catch (Exception)
            {
                _logger.LogError("陈子平执行任务发生错误");
                throw;
            }
        }
    }
}
